package com.crm.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.jdao.IDictionaryDAO;
import com.crm.model.pojo.Dictionary;

@Service
public class DictionaryServices {

	@Autowired
	private IDictionaryDAO dDao;
	@Autowired
	private DictionaryServices ds;
	
	/**
	 * 通过字典父类编号查询所有字典子类
	 * @param dictParentid
	 */
	public List<Dictionary> findByDictParentid(Integer dictParentid) {

		List<Dictionary> list=dDao.findByDictParentid(dictParentid);
		for (Dictionary dictionary : list) {
			System.out.println(dictionary.getDictName());
		}
		return list;
		
	}
	
	/**
	 * 将list的数据转换成JsonArray的数据
	 * @param <T>
	 * @param dictParentid
	 * @return
	 */
	public <T> JSONArray ListTransJSONArray(List<Dictionary> list) {
		JSONArray array =new JSONArray();

		for (Dictionary dictionary : list) {
			JSONObject obj = new JSONObject();			
			obj.put("value",dictionary.getDictId());
			obj.put("label",dictionary.getDictName());
			array.add(obj);

		}
		return array;	
	}
	
	/**
	 * 通过字典编号查询字典名称
	 * @param dictId
	 * @return
	 */
	public List<String> findByDictId(Integer dictId){
		
		return dDao.findByDictId(dictId);
	}
	/**
	 * 通过字典编号查找字典编号
	 * @param dictName
	 * @return
	 */
	public Dictionary findByDictIdOne(Integer dictId) {
		return dDao.findByDictOne(dictId);
	
	}
	
	/**
	 * 通过字典名称查找字典编号
	 * @param dictName
	 * @return
	 */
	public List<Integer> findByDictName(String dictName) {
		return dDao.findByDictName(dictName); 
	}
	
}
